﻿LoginWindow = Ext.extend(Ext.Window, {
    title: '登陆系统',
    width: 350,
    height: 220,
    collapsible: false,
    closable: false,
    modal: true,
    iconCls: 'user',
    id: 'loginwin',
    defaults: {
        border: false
    },
    buttonAlign: 'center',
    createFormPanel: function(){
        return new Ext.form.FormPanel({
            bodyStyle: 'padding-top:6px',
            defaultType: 'textfield',
            labelAlign: 'right',
            labelWidth: 55,
            labelPad: 0,
            frame: true,
            defaults: {
                allowBlank: false,
                selectOnFocus: true,
                width: 230,
                msgTarget: 'side'
            },
            items: [{
                xtype: 'panel',
                html: '<center><img src="static/images/desktop/login/logo_s.jpg" /></center>',
                width: 300
            }, {
                cls: 'username',
                name: 'name',
                fieldLabel: '帐号',
                allowBlank: false,
                blankText: '帐号不能为空'
            }, {
                cls: 'key',
                name: 'pwd',
                fieldLabel: '密码',
                allowBlank: false,
                blankText: '密码不能为空',
                inputType: 'password'
            }, {
                cls: 'rand',
                name: 'checkCode',
                id: 'randCode',
                fieldLabel: '验证码',
                width: 80,
                blankText: '验证码不能为空'
            }]
        });
    },
    login: function(){
    
    if (this.fp.form.isValid())//客户端先验证数据
    {
     this.fp.form.submit(
        {
            waitTitle: '提示',
            waitMsg: '正在登录......',
            url: 'admin/user.jhtml?method=login',
            method: 'POST',
            success: function(form, action){
                if (action.result.message == "logon") {
                     window.location.href = "index.jsp";
                   // dengluwin.close();
                   // dengluwin.destroy();
                }
                else if(action.result.message == "logoError") {
                	getAlertMessage("警告","用户名或密码有误!",Ext.MessageBox.WARNING);
                   form.reset();
                reloadcode();
                  return;
                }else if(action.result.message == "noRange"){
                	getAlertMessage("警告","IP地址不在合法范围之内!",Ext.MessageBox.WARNING);
                  form.reset();
                reloadcode();
                    return;
                }else if(action.result.message == "noRand"){
                	getAlertMessage("警告","验证码不正确!",Ext.MessageBox.WARNING);
                reloadcode();
                    return;
                }
            },
            failure: function(form, action){
                form.reset();
                reloadcode();
                if (action.failureType == Ext.form.Action.SERVER_INVALID) {
                    Ext.MessageBox.alert('警告', action.result.errors.msg);
                }
                if (action.failureType == Ext.form.Action.CLIENT_INVALID) {
                    Ext.MessageBox.alert('警告', "数据没有填写完整！");
                }
                if (action.failureType == null) {
                    Ext.Msg.alert("失败", action.result.message);
                }
            }
       
        });
    
    }  
    },
    regedit: function(){
        window.location.href = "#";
    },
    initComponent: function(){
        this.keys = {
            key: Ext.EventObject.ENTER,
            fn: this.login,
            scope: this
        };
        LoginWindow.superclass.initComponent.call(this);
        this.fp = this.createFormPanel();
        this.add(this.fp);
        this.addButton('登陆', this.login, this);
        this.addButton('重置', function(){
            this.fp.form.reset();
            reloadcode();
        }, this);
    }
});

function reloadcode(){
    var verify = document.getElementById('safecode');
    var dt = new Date();
    verify.setAttribute('src', 'include/randomNumber.jsp?a='+ dt);
}

var loginTT=function Login(){
    var loginwin = new LoginWindow();
    loginwin.show();
    var bd = Ext.getDom('randCode');
    var bd2 = Ext.get(bd.parentNode);
    bd2.createChild([{
        tag: 'span',
        html: '<a href="javascript:reloadcode();">'
    }, {
        tag: 'img',
        id: 'safecode',
        src: 'ValidateCode.aspx',
        align: 'absbottom'
    }, {
        tag: 'span',
        html: '</a>  <b>点击图片可刷新</b>'
    }]);
}

var dengluwin = null;
function denglu(){
    dengluwin = new LoginWindow();
    dengluwin.show();
    var bd = Ext.getDom('randCode');
    var bd2 = Ext.get(bd.parentNode);
    bd2.createChild([{
        tag: 'span',
        html: '<a style="padding-left:10px;" href="javascript:reloadcode();">'
    }, {
        tag: 'img',
        id: 'safecode',
        src: 'include/randomNumber.jsp',
        align: 'absbottom'
    }, {
        tag: 'span',
        html: '</a>  <b>点击图片可刷新</b>'
    }]);
}
